package com.bocloud.cmp.service.utils;

import java.util.UUID;

import org.apache.commons.lang.RandomStringUtils;

/**
 * 获取某长度的随机字符串
 * @author zjm
 * @date 2017年4月19日
 */
public class RandomUtil {
	
	/**
	 * 创建length长度的uuid
	 * @author zjm
	 * @date 2017年4月19日
	 *
	 * @param length
	 * @return
	 */
	public static String createUUID(int length) {
		String id = UUID.randomUUID().toString();

		id = DEKHash(id) + "";

		int diff = length - id.length();
		String randStr = RandomStringUtils.randomAlphabetic(length);
		for (int i = 0; i < diff; i++) {
			int randIndex = (int) (Math.random() * randStr.length());
			int index = (int) (Math.random() * id.length());
			id = id.substring(0, index) + randStr.charAt(randIndex) + id.substring(index, id.length());
		}
		return id;
	}

	private static int DEKHash(String str) {
		int hash = str.length();

		for (int i = 0; i < str.length(); i++) {
			hash = ((hash << 5) ^ (hash >> 27)) ^ str.charAt(i);
		}

		return (hash & 0x7FFFFFFF);
	}
	
}
